﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SysGroupAuthority
    {
        private readonly Chain.IDAL.SysGroupAuthority dal = new Chain.IDAL.SysGroupAuthority();

        public int Add(Chain.Model.SysGroupAuthority model)
        {
            return this.dal.Add(model);
        }

        public bool CheckChildGroup(int GroupID)
        {
            return this.dal.CheckChildGroup(GroupID);
        }

        public List<Chain.Model.SysGroupAuthority> DataTableToList(DataTable dt)
        {
            List<Chain.Model.SysGroupAuthority> list = new List<Chain.Model.SysGroupAuthority>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.SysGroupAuthority item = this.dal.DataRowToModel(dt.Rows[i]);
                    if (item != null)
                    {
                        list.Add(item);
                    }
                }
            }
            return list;
        }

        public bool Delete(int GAID)
        {
            return this.dal.Delete(GAID);
        }

        public bool DeleteList(int GroupID)
        {
            return this.dal.DeleteList(GroupID);
        }

        public bool DeleteList(string GAIDlist)
        {
            return this.dal.DeleteList(GAIDlist);
        }

        public bool Exists(int GAID)
        {
            return this.dal.Exists(GAID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.SysGroupAuthority GetModel(int GAID)
        {
            return this.dal.GetModel(GAID);
        }

        public List<Chain.Model.SysGroupAuthority> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int getModuleID(string AbsolutePath)
        {
            Chain.IDAL.SysGroupAuthority authority = new Chain.IDAL.SysGroupAuthority();
            return authority.getModuleID(AbsolutePath);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public bool isPowerReg(int groupID, int moduleID, int actionID)
        {
            DataTable table = this.dal.GetList(groupID, moduleID, actionID);
            return ((table.Rows.Count > 0) && this.dal.DataRowToModel(table.Rows[0]).ActionValue);
        }

        public bool Update(Chain.Model.SysGroupAuthority model)
        {
            return this.dal.Update(model);
        }

        public bool UpdateOrEndowPower(Chain.Model.SysGroupAuthority sysGroupAuthorityModel)
        {
            DataTable table = this.dal.GetList(sysGroupAuthorityModel.GroupID.Value, sysGroupAuthorityModel.ModuleID.Value, sysGroupAuthorityModel.ActionID.Value);
            if (table.Rows.Count > 0)
            {
                Chain.Model.SysGroupAuthority model = this.dal.DataRowToModel(table.Rows[0]);
                model.ActionValue = sysGroupAuthorityModel.ActionValue;
                return this.dal.Update(model);
            }
            if (this.Add(sysGroupAuthorityModel) <= 0)
            {
                return false;
            }
            return true;
        }
    }
}

